package com.rbs.controller;


import com.rbs.entity.User;
import com.rbs.service.UserService;
import com.rbs.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/selectByNaAndPwd")
    public String selectByNaAndPwd(String email, String password, HttpSession session){
        User user = userService.selectByNaAndPwd(email, password);
        if (user != null){
            session.setAttribute("user",user);
            return "redirect:/book/selectByRecommend.do";
        }else {
            return "redirect:/user/login_form.jsp";
        }
    }
    @RequestMapping("/insertUser")
    public String insertUser(User user,HttpSession session,String checkCode){
        String code = (String) session.getAttribute("code");
        if (code.equals(checkCode)){
            userService.insertUser(user);//user.getEmail()
            MailUtil.sendSimpleMail(user.getEmail(),"注册激活","<a href='http://localhost:8080/dangdang/user/relive.do?userId="+user.getUserId()+"'>点击激活</a>");
            return "redirect:/user/verify_form.jsp";
        }else {
            return "redirect:/user/register_form.jsp";
        }
    }
    @RequestMapping("/relive")
    public String relive(int userId,HttpSession session){
        userService.updateState(userId);
        User user = userService.selectById(userId);
        session.setAttribute("user",user);
        return "redirect:/user/register_ok.jsp";
    }
}
